package com.xmh.reptile.service;

import cn.hutool.core.io.FileUtil;
import com.xmh.reptile.util.DownloadUtil;
import com.xmh.reptile.util.RedisMockUtil;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DownloadService {

    public static final String key = "李环李长河";

    public static int errorCount = 0;

    public static long newPageNumber = Long.parseLong(String.valueOf(RedisMockUtil.get(key) == null ? 0 : RedisMockUtil.get(key)));

    public static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");

    public static void wgzz() throws InterruptedException {
        StringBuilder sb = new StringBuilder();
        try {
            Thread.sleep(2000);
            Document doc = Jsoup.connect("https://www.sxu3.com/book/7653.html").get();

            Elements list = doc.select(".list-charts li a");
            int i = 1;
            for (Element element : list) {
                String href = element.attr("href");

                String title = element.html();

                String pageNumber = StringUtils.substringAfterLast(href, "/read/7653_").split("\\.")[0];

                if (Long.parseLong(pageNumber) <= newPageNumber) {
                    continue;
                } else {
                    newPageNumber = Long.parseLong(pageNumber);
                }

                sb.append("\n\n====== 第").append(i++).append("章 ").append(title).append(" =====\n\n");

                Thread.sleep(2000);

                Document document = callAgain("https://www.sxu3.com" + href);

                Element select = document.select(".panel-default .content-body").first();

                String text = textHandle(select.html());

                sb.append(text);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (StringUtils.isNotEmpty(sb.toString())) {
            FileUtil.appendString(sb.toString(), new File("C:\\Users\\xmh\\Desktop\\小说\\" + key + "" + simpleDateFormat.format(new Date()) + ".txt"), "UTF-8");
            RedisMockUtil.set(key, newPageNumber);
        }
    }

    private static Document callAgain(String url) throws InterruptedException {
        try {
            Document document = Jsoup.connect(url).get();
            errorCount = 0;
            System.out.println("success get url !!!");
            return document;
        } catch (Exception e) {
            System.out.println("have a error, errorCount: " + errorCount);
            if (errorCount++ > 10) {
                throw new RuntimeException();
            }
            Thread.sleep(5000);
            return callAgain(url);
        }
    }

    private static String textHandle(String text) {
        return text.replace("&nbsp;&nbsp;&nbsp;&nbsp;", " ")
                .replace("一秒记住", "")
                .replace("\n" +
                        "<br>\n" +
                        "<br>\n" +
                        "<br>\n" +
                        "<br>", "\n")
                .replace("\n" +
                        "<br>\n" +
                        "<br>\n" +
                        "<br>\n" +
                        "<br>\n" +
                        "<br>", "\n")
                .replace("\n" +
                        "<br>\n" +
                        "<br>\n" +
                        "<br>", "\n")
                .replace("\n" +
                        "<br>\n" +
                        "<br>", "\n")
                .replace("<br>\n", "")
                .replace("<br>", "")
                .replace("&nbsp;", "")
                .replace("<span style=\"color:#333333;font-family:宋体, &quot;font-size:22px;background-color:#FBF4EC;\">", "")
                .replace("<span style=\"color:#555555;font-family:方正启体简体, &quot;font-size:25.3333px;background-color:#E9FAFF;\">", "")
                .replace("</span>", "")
                .replace("&nbsp;", "")
                .replace("<span style=\"color:#333333;font-family:宋体, &quot;font-size:22px;background-color:#FBF4EC;\">", "")
                .replace("<span style=\"color:#555555;font-family:方正启体简体, &quot;font-size:25.3333px;background-color:#E9FAFF;\">", "")
                .replace("</span>", "")
                .replace("\n" +
                        "    \n" +
                        "   \n" +
                        "   \n" +
                        "   \n" +
                        "     \n" +
                        "    ", "\n")
                .replace("\n" +
                        " \n" +
                        " ", "\n")
                .replace("\n" +
                        "   \n" +
                        "   ", "\n")
                .replace("记住网址uxzw．com", "")
                .replace("\n", "\n\t");
    }

}
